﻿
CREATE proc sp_phatsinhmaso 
@TienTo char(5)
as
declare @GT as int
declare @MA as char
declare @GTupdate char
select @GT=cast(substring(MaCT,6,3) as int) from DM_ChungTu_H
	where left(MaCT,5) = @TienTo
	and cast(month(getdate()) as char) = right(MaCT,2)
if(@GT is null)
	begin
	set @MA=(@TienTo + '001' + '/' + cast(month(getdate()) as char))
	update DM_TienTo
	set GiaTri='001'
	where TienTo=@TienTo
	select @MA
	end
else
	begin
	set @GTupdate = cast(@GT + 1 as char(3))
	while len(@GTupdate) <3
		begin
		set @GTupdate='0' + @GTupdate
		update DM_TienTo
		set GiaTri=@GTupdate
		where TienTo=@TienTo
		end
	set @MA=(@TienTo + @GTupdate + '/' + cast(month(getdate()) as char))
	select @MA
	end